/**  
* @Title: MembershipCardController.java  
* @Description: TODO(用一句话描述该文件做什么)  
* @author 张鑫
* @phone 17561783855  
* @date 2019年5月15日  
*/
package com.fortune.rainshine.system.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fortune.rainshine.common.BaseController;
import com.fortune.rainshine.common.utils.StringUtil;
import com.fortune.rainshine.system.service.MembershipCardService;
import com.fortune.rainshinecommon.base.MsgModel;
import com.fortune.rainshinecommon.base.ParmsModel;
import com.fortune.rainshinecommon.enums.ResultCode;
import com.fortune.rainshinecommon.model.system.CardModel;

/**  
* @ClassName: MembershipCardController  
* @Description: TODO(会员卡管理)  
* @author 张鑫
* @phone 17561783855
* @date 2019年5月15日  
*    
*/
@Controller
@RequestMapping("/system/card")	
public class CardController extends BaseController{
	private final static Logger logger = LoggerFactory.getLogger(CardController.class);
	
	@Autowired
	private MembershipCardService cardService;
	
	@RequiresPermissions("card:view")
	@RequestMapping()
	public String card(Model model) {
		return "card/cardList.html";
	}
	
	@RequestMapping("/editForm")
	public String addUser(Model model) {
		return "card/card_form.html";
	}
	
	/**
	 * @Title: add
	 * @Description: 添加会员卡
	 * @author zhangxin
	 * @phone 17561783855
	 * @date 2019年5月15日q
	 */
	@RequiresPermissions("card:add")
    @ResponseBody
    @RequestMapping("/add")
    public MsgModel add(@RequestBody CardModel model) {
		try {
			model.setUser(this.getLoginUser());
	    	return cardService.add(model);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.toString());
			return new MsgModel(ResultCode.FAILURE);
		}
    }
	
	/**
     * @Title: list
     * @Description: 列表
     * @author zhangxin
     * @date 2019年3月26日
     */
    @RequiresPermissions("card:view")
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public MsgModel list(Integer page, Integer limit, String searchKey, String searchValue) {
    	try {
    		if (page == null) {
                page = 0;
                limit = 0;
            }else {
            	page = page -1;
            }
            if (StringUtil.isBlank(searchValue)) {
                searchKey = null;
            }
           
            ParmsModel parm = new ParmsModel(page,limit,searchKey,searchValue);
            MsgModel ms = cardService.list(parm);
            return ms;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.toString());
			return new MsgModel(ResultCode.FAILURE);
		}
    	 
    }
    
    @RequiresPermissions("card:edit")
    @ResponseBody
    @RequestMapping("/update")
    public MsgModel update(@RequestBody CardModel model) {
    	try {
    		model.setUser(this.getLoginUser());

            return cardService.update(model);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.toString());
			return new MsgModel(ResultCode.FAILURE);
		}
    	
    }
    
    /**
     * @Title: delete
     * @Description: 删除
     * @author zhangxin
     * @phone 17561783855
     * @date 2019年3月28日
     */
    @RequiresPermissions("card:delete")
    @ResponseBody
    @RequestMapping("/delete")
    public MsgModel delete(@RequestBody CardModel model) {
    	try {
    		return cardService.delete(model);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.toString());
			return new MsgModel(ResultCode.FAILURE);
		}
        
    }
}
